namespace EFS.RC.CC.CHL.Writers
{
	internal class DB
	{
		// synk access
		private System.Object syncAccess = new System.Object();

		// DB Connection
		private EFS.DB.Connection connection = null;

		/// Property Instance (singleton): Get
		public static DB Instance { get; private set; }

		/// <summary>
		/// Static constructor to init local instance
		/// </summary>
		static DB()
		{
			Instance = new DB();
		}

		/// <summary>
		/// Contructor
		/// </summary>
		private DB()
		{
			connection = EFS.RC.CC.CH.QRDConnection.New;
		}

   		// Log entry
		public void Write(EFS.RC.CC.CHL.Loggers.Messages.Message message)
		{
			// ~ commented for performance ~  EFS.EventLog.Log.LogExtended("Entering lock", new System.Diagnostics.StackTrace());
			lock (syncAccess)
			{
				try
				{
					// CONVERSATION
					if (message is EFS.RC.CC.CHL.Loggers.Messages.Conversation)
					{
						// cast
						EFS.RC.CC.CHL.Loggers.Messages.Conversation conversationObj = message as EFS.RC.CC.CHL.Loggers.Messages.Conversation;

						// create into DB
						EFS.RC.CC.CH.Tables.EventLogs.Create(
							conversationObj.Occurred,
							conversationObj.ConversationGuid,
							conversationObj.EndPointID,
							conversationObj.CustomerUri,
							conversationObj.AgentADGuid,
							conversationObj.SupervisorADGuid,
							conversationObj.HWPartyID,
							conversationObj.HWApplicantID,
							conversationObj.ConversationClassID,
							conversationObj.EventCode,
							connection);
					}
					else 	
					// ACTIVITY
					if (message is EFS.RC.CC.CHL.Loggers.Messages.UserActity)
					{
						// cast
						EFS.RC.CC.CHL.Loggers.Messages.UserActity userActivityMessage = message as EFS.RC.CC.CHL.Loggers.Messages.UserActity;

						// create into DB
						EFS.RC.CC.CH.Tables.UserActivity.Create(
							userActivityMessage.Occurred,
							userActivityMessage.UserADGuid,
							userActivityMessage.ActivityCode,
							userActivityMessage.CustomActivityDescription,
							connection);
					}
					else
					// QUESTIONARE
					if (message is EFS.RC.CC.CHL.Loggers.Messages.QuestionareSessionAnswer)
					{
						// cast
						EFS.RC.CC.CHL.Loggers.Messages.QuestionareSessionAnswer questionareSessionAnswer = message as EFS.RC.CC.CHL.Loggers.Messages.QuestionareSessionAnswer;

						// create into DB
						EFS.RC.CC.CH.Tables.QuestionareSessionAnswer.Create(
							questionareSessionAnswer.Occurred,
							questionareSessionAnswer.ConversationGuid,
							questionareSessionAnswer.QuestionId,
							questionareSessionAnswer.AanswerCode,
							connection);
					}
				}
				catch (System.Exception ex)
				{
					EFS.EventLog.Log.LogException(ex, null);
				}
			}
		}
	}

}